//
// Created by 孟令涛 on 24-7-19.
//
#include "FireSensorTask.h"
#include "GlobalVars.h"
#include "CarMovementTask.h"
#include "CarControlTask.h"

int Fire_Value[3];
int last_fire_state = 0;

int get_turn() {
//    if (fire_state) {
    if (Fire_Value[0] == 1 && Fire_Value[1] == 1 && Fire_Value[2] == 1) {
        last_fire_state = 0;
        return 0;
    } else if (Fire_Value[0] == 1) {
        last_fire_state = 5;
        return 5;
    } else if (Fire_Value[2] == 1) {
        last_fire_state = 6;
        return 6;
    } else if (Fire_Value[1] == 1) {
        last_fire_state = 0;
        return 0;
    } else {
        return 1;
    }
//    }

}

void get_fire() {
//    Fire_Value[0] = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4);
//    Fire_Value[1] = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5);
//    Fire_Value[2] = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6);
    if (!(Fire_Value[0] == 0 && Fire_Value[1] == 0 && Fire_Value[2] == 0)) {
//        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
        fire_state = 1;
        r = 500;
//        g = 500;
//        b = 500;
    } else {
//        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);//关闭蜂鸣器
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);//关闭风扇
        fire_state = 0;
    }
}

void FireSensorTask(void *argument) {
//    if (fireSensor) {
//        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
//    } else {
//        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
//
//    }
    while (1) {
        r = 0;
        g = 0;
        b = 0;
        if (!fire_state) {
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
        }

        Fire_Value[2] = adcValues[0] >= 0x0EFF ? 1 : 0;
        Fire_Value[1] = adcValues[1] >= 0x0FFF ? 1 : 0;
        Fire_Value[0] = adcValues[2] >= 0x0E8F ? 1 : 0;
//        if (Fire1 > 0x600 || Fire3 > 0x600 || Fire5 > 0x600) {
//            fire_state = 1;//开启灭火模式

        get_fire();

        fire_movement_state = get_turn();
        if (!fire_state) {
            if (last_fire_state == 5) {
                fire_movement_state = 6;
                while (!(!carControlLeft1 && carControlLeft2 && carControlRight1 && !carControlRight2));
            } else if (fire_movement_state == 6) {
                fire_movement_state = 5;
                while (!(!carControlLeft1 && carControlLeft2 && carControlRight1 && !carControlRight2));
            }
        }

//            fire_state = 0;
//        }
        osDelay(10);
    }


}
